package goRpc

import (
	"github.com/flipped-aurora/gin-vue-admin/server/goRpc/req"
	"github.com/flipped-aurora/gin-vue-admin/server/goRpc/res"
)

type ClassifiedRpc struct {
}

func (a ClassifiedRpc) Save(classifiedReq *req.ClassifiedReq, reply *res.Response) error {

	archiveType, err := archiveTypeService.GetArchiveTypeByName(classifiedReq.TypeName)
	if err != nil {
		archiveType.Name = classifiedReq.TypeName
		err := archiveTypeService.CreateArchiveType(&archiveType)
		if err != nil {
			return err
		}
	}

	location, err := archiveLocationService.GetArchiveLocationByName(classifiedReq.LocationName)
	if err != nil {
		location.Name = classifiedReq.LocationName
		err = archiveLocationService.CreateArchiveLocation(&location)
		if err != nil {
			return err
		}
	}

	file, err := archiveFileService.GetArchiveFileByName(classifiedReq.ArchiveName)
	if err != nil {
		typeId := int(archiveType.ID)
		file.TypeId = &typeId
		locationId := int(location.ID)
		file.LocationId = &locationId
		file.Name = classifiedReq.ArchiveName
		err = archiveFileService.CreateArchiveFile(&file)
		if err != nil {
			return err
		}
	} else {
		*file.TypeId = int(archiveType.ID)
		*file.LocationId = int(location.ID)
		file.Name = classifiedReq.ArchiveName
		err := archiveFileService.UpdateArchiveFile(file)
		if err != nil {
			return err
		}
	}

	return nil
}
